import requests
from lxml import etree
import time
"""
"commit":"Sign+in",
"authenticity_token":"8nD42v7ECaS0i6KsbC6aF+KCr7UP0z64HBH9SBcjy0IkrFA1t4BNVefMUABufAKuGhM3W8c1HoGj9wUCxq1+DA==",需要构件
"ga_id":"1012293138.1571402591",
"login":"",账号
"password":"",密码
"webauthn-support":"supported",
"webauthn-iuvpaa-support":"unsupported",
"return_to":"",
"required_field_cb05":"",
"timestamp":"1585911010557", 时间戳要生成
"timestamp_secret":"29ebb8412ed30062429ba42480d368d5585e1707668e3220eacc31704aa63af9"},
"""
"""
可以访问https://github.com/login来实时获取authenticity_token，timestamp，timestamp_secret
"""
get_session="https://github.com/login"
headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0"}
# 构建session对象
session=requests.session()
session.headers.update(headers)
res=session.get(get_session)
# 获得3个信息
res.encoding='utf-8'
html=etree.HTML(res.text)
"""
<input type="text" name="required_field_0165" hidden="hidden" class="form-control" /><input type="hidden" name="timestamp" value="1585912011159" class="form-control" /><input type="hidden" name="timestamp_secret" value="839b78deebaf76915a9429d53d37212775c5a978ef5e1b6771ad74f039ae397a" class="form-control" />
"""
authenticity_token=html.xpath("//input[@name='authenticity_token']/@value")[0]
timestamp=html.xpath("//input[@name='timestamp']/@value")[0]
# timestamp=str(int(time.time()*1000))
timestamp_secret=html.xpath("//input[@name='timestamp_secret']/@value")[0]
# print(authenticity_token,timestamp,timestamp_secret)
# 进行登录
login_url='https://github.com/session'
form={
    "commit":"Sign+in",
    "authenticity_token":authenticity_token,
    "ga_id":"1012293138.1571402591",
    "login":"952302908@qq.com",
    "password":"extreme0710",
    "webauthn-support":"supported",
    "webauthn-iuvpaa-support":"unsupported",
    "return_to":"",
    "required_field_cb05":"",
    "timestamp":timestamp,
    "timestamp_secret":timestamp_secret
}
res=session.post(login_url,data=form)
if "yozuriha" in res.text:
    print("登录成功")